package com.mobilepoc.jersey.storage;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mobilepoc.jersey.model.Transaction;
import com.mobilepoc.jersey.model.TransactionWrapper;

public class TransactionStorage
{
   private static final long ONE_DAY_IN_MS = 86400000L;
   
   //Key=AccountId, Value=Transaction
   private static Map<String, List<Transaction>> transactions = new HashMap<String, List<Transaction>>();

   static {
      
         Transaction t1=new Transaction("5500444","Icabutiken", new Date(), -250);
         Transaction t2=new Transaction("5500445","Systembolaget", new Date(new Date().getTime()-2*ONE_DAY_IN_MS), -250);
         Transaction t3=new Transaction("5500446","Willys", new Date(new Date().getTime()-4*ONE_DAY_IN_MS), -1150);
         Transaction t4=new Transaction("5500447","Restaurangen", new Date(new Date().getTime()-5*ONE_DAY_IN_MS), -850);
         Transaction t5=new Transaction("5500448","Sportaffären", new Date(new Date().getTime()-5*ONE_DAY_IN_MS), -2250);
         Transaction t6=new Transaction("5500449","Lön", new Date(new Date().getTime()-6*ONE_DAY_IN_MS), 32250);
         Transaction t7=new Transaction("5500450","Icabutiken", new Date(new Date().getTime()-7*ONE_DAY_IN_MS), -650);
         Transaction t8=new Transaction("5500451","Kiosken", new Date(new Date().getTime()-8*ONE_DAY_IN_MS), -50);
         Transaction t9=new Transaction("5500452","Pressbyrån", new Date(new Date().getTime()-9*ONE_DAY_IN_MS), -24);
         Transaction t10=new Transaction("5500453","Icabutiken", new Date(new Date().getTime()-9*ONE_DAY_IN_MS), -250);
         Transaction t11=new Transaction("5500454","Konsumbutiken", new Date(new Date().getTime()-10*ONE_DAY_IN_MS), -250);
         Transaction t12=new Transaction("5500455","Icabutiken", new Date(new Date().getTime()-10*ONE_DAY_IN_MS), -246);
         Transaction t13=new Transaction("5500456","Pablos Pannkakor", new Date(new Date().getTime()-10*ONE_DAY_IN_MS), -250);
         Transaction t14=new Transaction("5500457","Amazon", new Date(new Date().getTime()-11*ONE_DAY_IN_MS), -1150);
         Transaction t15=new Transaction("5500458","Icabutiken", new Date(new Date().getTime()-11*ONE_DAY_IN_MS), -398);
         Transaction t16=new Transaction("5500459","Hyra", new Date(new Date().getTime()-11*ONE_DAY_IN_MS), -12049);
         Transaction t17=new Transaction("5500460","Bankomat", new Date(new Date().getTime()-11*ONE_DAY_IN_MS), -300);
         Transaction t18=new Transaction("5500461","Pressbyrån", new Date(new Date().getTime()-12*ONE_DAY_IN_MS), -650);
         Transaction t19=new Transaction("5500462","Svensk Lokaltrafik", new Date(new Date().getTime()-14*ONE_DAY_IN_MS), -790);
         Transaction t20=new Transaction("5500463","Pressbyrån", new Date(new Date().getTime()-15*ONE_DAY_IN_MS), -24);
         Transaction t21=new Transaction("5500464","Icabutiken", new Date(new Date().getTime()-15*ONE_DAY_IN_MS), -270);
         Transaction t22=new Transaction("5500465","Konsumbutiken", new Date(new Date().getTime()-16*ONE_DAY_IN_MS), -19);
         Transaction t23=new Transaction("5500466","Icabutiken", new Date(new Date().getTime()-19*ONE_DAY_IN_MS), -657);
         Transaction t24=new Transaction("5500467","Systembolaget", new Date(new Date().getTime()-20*ONE_DAY_IN_MS), -250);
         Transaction t25=new Transaction("5500468","Willys", new Date(new Date().getTime()-21*ONE_DAY_IN_MS), -1150);
         Transaction t26=new Transaction("5500469","Restaurangen", new Date(new Date().getTime()-24*ONE_DAY_IN_MS), -850);
         Transaction t27=new Transaction("5500470","Sportaffären", new Date(new Date().getTime()-27*ONE_DAY_IN_MS), -2250);
         Transaction t28=new Transaction("5500471","Lön", new Date(new Date().getTime()-36*ONE_DAY_IN_MS), 32250);
         Transaction t29=new Transaction("5500472","Icabutiken", new Date(new Date().getTime()-36*ONE_DAY_IN_MS), -650);
         Transaction t30=new Transaction("5500473","Kiosken", new Date(new Date().getTime()-37*ONE_DAY_IN_MS), -50);
         Transaction t31=new Transaction("5500474","Pressbyrån", new Date(new Date().getTime()-38*ONE_DAY_IN_MS), -24);
         Transaction t32=new Transaction("5500475","Hyra", new Date(new Date().getTime()-41*ONE_DAY_IN_MS), -12049);
         Transaction t33=new Transaction("5500476","Konsumbutiken", new Date(new Date().getTime()-43*ONE_DAY_IN_MS), -250);
         Transaction t34=new Transaction("5500477","Svensk Lokaltrafik", new Date(new Date().getTime()-44*ONE_DAY_IN_MS), -790);
         Transaction t35=new Transaction("5500478","Systembolaget", new Date(new Date().getTime()-45*ONE_DAY_IN_MS), -250);
         Transaction t36=new Transaction("5500479","Willys", new Date(new Date().getTime()-45*ONE_DAY_IN_MS), -1150);
         Transaction t37=new Transaction("5500480","Restaurangen", new Date(new Date().getTime()-47*ONE_DAY_IN_MS), -850);
         Transaction t38=new Transaction("5500481","Sportaffären", new Date(new Date().getTime()-48*ONE_DAY_IN_MS), -2250);
         Transaction t39=new Transaction("5500482","Bankomat", new Date(new Date().getTime()-50*ONE_DAY_IN_MS), -500);
         Transaction t40=new Transaction("5500483","Icabutiken", new Date(new Date().getTime()-52*ONE_DAY_IN_MS), -650);
         Transaction t41=new Transaction("5500484","Kiosken", new Date(new Date().getTime()-53*ONE_DAY_IN_MS), -50);
         Transaction t42=new Transaction("5500485","Pressbyrån", new Date(new Date().getTime()-57*ONE_DAY_IN_MS), -24);
         Transaction t43=new Transaction("5500486","Icabutiken", new Date(new Date().getTime()-58*ONE_DAY_IN_MS), -250);
         Transaction t44=new Transaction("5500487","Konsumbutiken", new Date(new Date().getTime()-58*ONE_DAY_IN_MS), -250);
         Transaction t45=new Transaction("5500488","Icabutiken", new Date(new Date().getTime()-58*ONE_DAY_IN_MS), -250);
         Transaction t46=new Transaction("5500489","Systembolaget", new Date(new Date().getTime()-59*ONE_DAY_IN_MS), -250);
         Transaction t47=new Transaction("5500490","Willys", new Date(new Date().getTime()-60*ONE_DAY_IN_MS), -1150);
         Transaction t48=new Transaction("5500491","Restaurangen", new Date(new Date().getTime()-62*ONE_DAY_IN_MS), -850);
         Transaction t49=new Transaction("5500492","Sportaffären", new Date(new Date().getTime()-63*ONE_DAY_IN_MS), -2250);
         Transaction t50=new Transaction("5500493","Lön", new Date(new Date().getTime()-66*ONE_DAY_IN_MS), 32250);
         Transaction t51=new Transaction("5500494","Icabutiken", new Date(new Date().getTime()-67*ONE_DAY_IN_MS), -650);
         Transaction t52=new Transaction("5500495","Kiosken", new Date(new Date().getTime()-68*ONE_DAY_IN_MS), -50);
         Transaction t53=new Transaction("5500496","Pressbyrån", new Date(new Date().getTime()-69*ONE_DAY_IN_MS), -24);
         Transaction t54=new Transaction("5500497","Icabutiken", new Date(new Date().getTime()-69*ONE_DAY_IN_MS), -250);
         Transaction t55=new Transaction("5500498","Konsumbutiken", new Date(new Date().getTime()-70*ONE_DAY_IN_MS), -250);
         
         List<Transaction> l1=new ArrayList<Transaction>();
         l1.add(t1);
         l1.add(t2);
         l1.add(t6);
         l1.add(t8);
         l1.add(t12);
         l1.add(t13);
         l1.add(t14);
         l1.add(t15);
         l1.add(t16);
         l1.add(t17);
         l1.add(t18);
         l1.add(t19);
         l1.add(t20);
         l1.add(t21);
         l1.add(t22);
         l1.add(t23);
         l1.add(t24);
         l1.add(t25);
         l1.add(t26);
         l1.add(t27);
         l1.add(t28);
         l1.add(t29);
         l1.add(t30);
         l1.add(t31);
         l1.add(t32);
         l1.add(t33);
         l1.add(t34);
         l1.add(t35);
         l1.add(t36);
         l1.add(t37);
         l1.add(t38);
         l1.add(t39);
         l1.add(t40);
         l1.add(t41);
         l1.add(t42);
         l1.add(t43);
         l1.add(t44);
         l1.add(t45);
         l1.add(t46);
         l1.add(t47);
         l1.add(t48);
         l1.add(t49);
         l1.add(t50);
         l1.add(t51);
         l1.add(t52);
         l1.add(t53);
         l1.add(t54);
         l1.add(t55);
         transactions.put("123465", l1);

         List<Transaction> l2=new ArrayList<Transaction>();
         l2.add(t3);
         l2.add(t5);
         l2.add(t11);
         transactions.put("654321", l2);
         
   
         List<Transaction> l3=new ArrayList<Transaction>();
         l3.add(t7);
         l3.add(t3);
         l3.add(t4);
         l3.add(t5);
         l3.add(t11);
         transactions.put("999999", l3);
   
         List<Transaction> l4=new ArrayList<Transaction>();
         l4.add(t9);
         l4.add(t10);
         transactions.put("234567", l4);
         

   }

   public static TransactionWrapper getTransactions(String accountId, int transactionsCurrentlyLoaded) {
	   System.out.println("1 " + transactions.get("123465").size());
	   System.out.println("2 " + transactions.get("654321").size());
	   System.out.println("3 " + transactions.get("999999").size());
	   System.out.println("4 " + transactions.get("234567").size());
	   List<Transaction> transactionsFromAccount = transactions.get(accountId);
	   int numberOfAvailableTransactions = transactionsFromAccount.size();
	   
	   int startIndex = transactionsCurrentlyLoaded;
	   int endIndex = Math.min(transactionsCurrentlyLoaded+10,numberOfAvailableTransactions);
	   System.out.println("StartIndex: " + startIndex);
	   System.out.println("EndIndex: " + endIndex);
	   System.out.println("Number of total transactions: " + numberOfAvailableTransactions);
	   List<Transaction> transactionsToRetrieve = transactionsFromAccount.subList(startIndex, endIndex);
	   
	   //return transactionsToRetrieve;
	   return new TransactionWrapper(numberOfAvailableTransactions,transactionsToRetrieve);
   }
}
